package com.mydemo.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.function.Consumer;

import static org.springframework.ai.chat.memory.ChatMemory.CONVERSATION_ID;

@RestController
public class PersistentController {


    /**
     * 通过 chatclient 实现流式响应
     */
    @Resource(name="deepSeekChatClient")
    private ChatClient deepSeekClient;
    @Resource(name="qwenChatClient")
    private ChatClient qwenClient;

    @GetMapping(value = "/persistent")
    public Flux<String> chat(String msg,String userId){
        return qwenClient.prompt(msg).advisors(new Consumer<ChatClient.AdvisorSpec>() { //消费形的 advisor
            @Override
            public void accept(ChatClient.AdvisorSpec advisorSpec) {

                advisorSpec.param(CONVERSATION_ID,userId);

            }
        }).stream().content() ;
    }

    @GetMapping(value = "/persistent2", produces = "text/event-stream;charset=UTF-8")
    public Flux<String> chat2(String msg,String userId){
        //消费形的 advisor
        return qwenClient.prompt(msg)
                        .advisors(advisorSpec ->
                            advisorSpec.param(CONVERSATION_ID,userId))
                        .stream().content() ;
    }

}
